package cn.edu.seu.cose.qol.ws.resource.util;

import java.io.IOException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.restlet.ext.xml.DomRepresentation;
import org.xml.sax.InputSource;

public class XmlObjectReader {
	
	private static XmlObjectReader _instance;
	
	public static XmlObjectReader getInstance() {
		if (_instance == null) {
			_instance = new XmlObjectReader();
		}
		
		return _instance;
	}
	
	protected XmlObjectReader() {}
	
	public Object getXmlObject(DomRepresentation representation, Class<?> cls)
			throws JAXBException, IOException {
		InputSource inputSource = representation.getInputSource();
		
		JAXBContext jaxbContext = JAXBContext.newInstance(cls);
		Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
		
		return jaxbUnmarshaller.unmarshal(inputSource);
	}

}
